package com.itheima.service.impl;

import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupDao;
import com.itheima.mapper.SetmealDao;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class SetmealServiceimpl implements SetmealService {

    @Autowired
    SetmealDao setmealDao;
    @Autowired
    CheckGroupDao checkGroupDao;

    @Override
    public void add(Setmeal setmeal,Integer[] checkGroupIds) {

        //查询检查组编码与检查组名字是否重复
        List<Setmeal> setmeal1 = setmealDao.findByCodeAndName(setmeal.getCode(), setmeal.getName());
        if(setmeal1!=null && setmeal1.size() > 0){
            throw new RuntimeException("检查组编码或名称重复");
        }

        setmealDao.add(setmeal);
        setmealDao.setSetmealAndCheckGroup(setmeal.getId(), checkGroupIds);
    }

    @Override
    public void edit(Setmeal setmeal,Integer[] checkGroupIds) {
        //更新套餐表信息
        setmealDao.edit(setmeal);
//        更新套餐和检查组关系表信息
        setmealDao.deleteSetmealAndCheckGroup(setmeal.getId());
        setmealDao.setSetmealAndCheckGroup(setmeal.getId(), checkGroupIds);
    }

    @Override
    public Setmeal findById(Integer id) {

        Setmeal setmeal = setmealDao.findById(id);
        List<Integer> ids = setmealDao.findCheckGroupIdsBySetmealId(id);
        List<CheckGroup> checkGroups = new ArrayList<>();
        for(Integer id1 : ids){
            CheckGroup checkGroup = checkGroupDao.findById(id1);
            checkGroups.add(checkGroup);
        }
        setmeal.setCheckGroups(checkGroups);
        return setmeal;

    }

    @Override
    public List<Setmeal> findPage(QueryPageBean queryPageBean) {

        int start = (queryPageBean.getCurrentPage() - 1) * queryPageBean.getPageSize();
        int end = start + queryPageBean.getPageSize();
        List<Setmeal> setmeals = setmealDao.findPage(start, end, queryPageBean.getQueryString());
        for(Setmeal setmeal : setmeals){
            List<Integer> ids = setmealDao.findCheckGroupId(setmeal.getId());
            List<CheckGroup> CheckGroups = new ArrayList<>();
            for(Integer id : ids){

                CheckGroup checkGroup = checkGroupDao.findById(id);
                CheckGroups.add(checkGroup);

            }
            setmeal.setCheckGroups(CheckGroups);
        }

        return setmeals;

    }

    @Override
    public Long findCount() {

        return setmealDao.findCount() != null ? setmealDao.findCount() : 0L;
    }

    @Override
    public List<String> findAllImgURL() {

        return setmealDao.findAllImgURL();

    }

    @Override
    public List<Setmeal> getSetmeal() {

        List<Setmeal> setmealList= setmealDao.getSetmeal();
        for(Setmeal setmeal : setmealList){

            List<Integer> ids = setmealDao.findCheckGroupId(setmeal.getId());
            List<CheckGroup> checkGroups = new ArrayList<>();
            for(Integer id : ids){
                CheckGroup checkGroup = checkGroupDao.findById(id);
                checkGroups.add(checkGroup);
            }
            setmeal.setCheckGroups(checkGroups);
        }

        return setmealList;

    }


}
